package com.kkd.kkdapp.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.DealDetailsActivity;
import com.kkd.kkdapp.activity.WhatIsAPiggyBankActivity;
import com.kkd.kkdapp.http.entity.DealDetailData;
import com.kkd.kkdapp.http.entity.DealDetailItem;
import com.kkd.kkdapp.http.entity.DealDetailItemBean;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.pinnedsection.PinnedSectionRefreshListView;
import com.kkd.kkdapp.pinnedsection.PinnedSectionRefreshListView.IXListViewListener;
import com.kkd.kkdapp.pinnedsection.WarnDetailAdapter;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.Time_Date;

public class DealDetailPinnerFragment extends Fragment implements
		IXListViewListener {

	private final int CODE_ZREO = 1;
	private final int CODE_ELSE = 3;

	private View mContentView;
	WarnDetailAdapter mAdapter;
	private DealDetailsActivity mDealDetailsActivity;
	private String tradeType;
	private int status;
	private PinnedSectionRefreshListView mListView;
	private int pageNO = 1;
	private int pageSize = 20;
	View noContent;
	private ImageView mNullImg;
	private TextView mNullTv;
	private boolean REFRESHFLAG = true;
	private boolean isShowToastOnRefresh = false;
	private List<DealDetailItem> datalist = new ArrayList<DealDetailItem>();

	Toast mToast;
	int pageCount;
	String time = "";

	private DealDetailData data;
	private ArrayList<DealDetailItemBean> list_bean = new ArrayList<DealDetailItemBean>();;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			Time_Date time_Date;
			String time;
			switch (msg.what) {
			case CODE_ZREO:
				if (REFRESHFLAG) {
					tv_num_yue.setText(data.getBalance());
				}

				int pageNo = data.getPageNO();
				int pageCount = data.getPageCount();

				if (pageNo > pageCount) {
					if (pageNO == 1) {
						mListView.stopLoadMore(-1);
					} else {
						mListView.stopLoadMore(1);
					}
				} else {
					mListView.setNoMore(false);
					if (pageNO == 1) {
						datalist.clear();
					}
					datalist.addAll(data.getData1());
					list_bean.clear();
					list_bean.addAll(DealDetailItemBean.getData(datalist));
					mAdapter.notifyDataSetChanged();
					if (data.getData1().size() < pageSize) {
						mListView.stopLoadMore(1);
					} else {
						mListView.stopLoadMore(0);
					}
				}
				time_Date = new Time_Date();
				time = time_Date.get_Year_Day();
				mListView.stopRefresh();
				if (REFRESHFLAG) {
					if (datalist != null) {
						if (datalist.size() == 0) {
							mNullImg.setImageResource(R.drawable.img_null);
							mNullTv.setText("您暂无任何交易记录");
							mNullImg.setVisibility(View.VISIBLE);
							mNullTv.setVisibility(View.VISIBLE);
						} else {
							mNullImg.setVisibility(View.GONE);
							mNullTv.setVisibility(View.GONE);
						}
					}
				}

				if (!isShowToastOnRefresh) {
					isShowToastOnRefresh = true;
				}
				break;
			case CODE_ELSE:
				time_Date = new Time_Date();
				time = time_Date.get_Year_Day();
				if (REFRESHFLAG) {
					if (datalist != null) {
						if (datalist.size() == 0) {
							if (msg.arg1 == KkdHttpClient.NO_NET) {
								mNullImg.setImageResource(R.drawable.img_null_net);
								mNullTv.setText("网络错误，请稍候下拉刷新重试");
							} else {
								mNullImg.setImageResource(R.drawable.img_null_net);
								mNullTv.setText("服务异常，请稍候下拉刷新重试");
							}
							mNullImg.setVisibility(View.VISIBLE);
							mNullTv.setVisibility(View.VISIBLE);
						} else {
							mNullImg.setVisibility(View.GONE);
							mNullTv.setVisibility(View.GONE);
						}
					}
				}

				if (pageNO == 1) {
					if (datalist != null && datalist.size() > 0) {
						mListView.stopLoadMore(1);
					} else {
						mListView.stopLoadMore(-1);
					}
				} else {
					mListView.stopLoadMore(1);
				}
				mListView.stopRefresh();
				if (datalist.size() == 0) {
					if (msg.arg1 == KkdHttpClient.NO_NET) {
						mNullImg.setImageResource(R.drawable.img_null_net);
						mNullTv.setText("网络错误，请稍候下拉刷新重试");
					} else {
						mNullImg.setImageResource(R.drawable.img_null_net);
						mNullTv.setText("服务异常，请稍候下拉刷新重试");
					}
					mNullImg.setVisibility(View.VISIBLE);
					mNullTv.setVisibility(View.VISIBLE);
				}

				String desc = (String) msg.obj;
				if (isShowToastOnRefresh) {
					if (!TextUtils.isEmpty(desc)) {
						AppUtil.makeToastForCustomer(mDealDetailsActivity, desc);
					}
				} else {
					isShowToastOnRefresh = true;
				}

				break;
			}

		};
	};
	private TextView tv_num_yue;
	private RelativeLayout rel_tip;

	public static DealDetailPinnerFragment newInstance(String tradeType,
			int status) {
		DealDetailPinnerFragment fragment = new DealDetailPinnerFragment();
		Bundle mBundle = new Bundle();
		mBundle.putString("tradeType", tradeType);
		mBundle.putInt("status", status);
		fragment.setArguments(mBundle);
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mDealDetailsActivity = (DealDetailsActivity) activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mContentView = inflater.inflate(
				R.layout.fragment_pinner_classic_header_with_list_view, null);
		tradeType = getArguments().getString("tradeType");
		status = getArguments().getInt("status");
		mNullImg = (ImageView) mContentView.findViewById(R.id.id_no_content);
		mNullTv = (TextView) mContentView.findViewById(R.id.id_no_content_text);
		tv_num_yue = (TextView) mContentView.findViewById(R.id.tv_num_yue); // 账户余额
		rel_tip = (RelativeLayout) mContentView.findViewById(R.id.rel_tip); // 点击查看余额收益
		rel_tip.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				startActivity(new Intent(mDealDetailsActivity,
						WhatIsAPiggyBankActivity.class));
			}
		});
		mListView = (PinnedSectionRefreshListView) mContentView
				.findViewById(R.id.deal_detail_list);
		mListView.setDividerHeight(0);
		mAdapter = new WarnDetailAdapter(list_bean, getActivity());
		mListView.setAdapter(mAdapter);
		mListView.setPullLoadEnable(true);
		mListView.setXListViewListener(this);

		requestDealDetails(tradeType, status);
		return mContentView;
	}

	public void requestDealDetails(String tradeType, int status) {
		KkdHttpMagager.getInstance().getDealDetailsManager(pageNO, pageSize,
				tradeType, status, DealDetailData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						data = (DealDetailData) responseBody;
						handler.sendEmptyMessage(CODE_ZREO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = Message.obtain();
						msg.what = CODE_ELSE;
						msg.obj = desc;
						msg.arg1 = statusCode;
						handler.sendMessage(msg);
					}
				});
	}

	// @Override
	// public void onRefresh() {
	// pageNO = 1;
	// requestDealDetails(tradeType, status);
	// REFRESHFLAG = true;
	// }

	// @Override
	// public void onLoadMore() {
	// pageNO++;
	// REFRESHFLAG = false;
	// requestDealDetails(tradeType, status);
	//
	// }

	public void showToast(String hintLanguage) {
		AppUtil.makeToastForCustomer(mDealDetailsActivity, hintLanguage);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mToast != null) {
			mToast.cancel();
			mToast = null;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mToast != null) {
			mToast.cancel();
			mToast = null;
		}
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		requestDealDetails(tradeType, status);
		REFRESHFLAG = true;
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		REFRESHFLAG = false;
		requestDealDetails(tradeType, status);
	}
}
